<%@ Page Language="C#" MasterPageFile="~/UI/Masters/Content.master" AutoEventWireup="true" CodeBehind="Explorer.aspx.cs" Inherits="Stratman.Web.VSTS.UI.Pages.Timesheet.Explorer" %>

<asp:Content ID="c" ContentPlaceHolderID="c" runat="server">
    <script language="javascript" type="text/javascript">
        var changeQueue = new Array();
        var completed = "";
        var approved = "";
        var savingMessageDiv = null;
        var savingMessageHtmlDiv = null;
        var savingShim = null;
        var timeEntryMetadata = new Object();
        
        function ShowSavingMessage(message)
	    {
		    if(!savingMessageDiv)
		    {
			    savingMessageDiv = document.createElement("div");
			    savingMessageDiv.style.position = "absolute";
			    savingMessageDiv.style.zIndex = "10000";
			    savingMessageDiv.style.width = "100%";
			    savingMessageDiv.style.height = "100%";
    			
			    if(document.all)
			    {
				    savingMessageDiv.style.backgroundColor = "#CCCCCC";
				    savingMessageDiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha( Opacity=50, FinishOpacity=0, Style=0, StartX=0,  FinishX=100, StartY=0, FinishY=100)";
			    }
			    else 
			    {
				    savingMessageDiv.style.backgroundImage = "url(" + CommonUrls.TransparentBackground + ")";
			    }
    			
			    document.body.appendChild(savingMessageDiv);
		    }
    		
		    savingMessageDiv.style.left = "0px";
		    savingMessageDiv.style.top = "0px";
		    savingShim = JsUtility.showShim(savingShim, savingMessageDiv);
    		
		    if(!savingMessageHtmlDiv)
		    {
			    savingMessageHtmlDiv = document.createElement("div");
			    savingMessageHtmlDiv.style.position = "absolute";
			    savingMessageHtmlDiv.style.zIndex = "10001";
			    savingMessageHtmlDiv.style.width = 250;
			    savingMessageHtmlDiv.style.height = 150;
			    savingMessageHtmlDiv.style.backgroundColor = "#F8F8F8";
			    savingMessageHtmlDiv.style.filter = "progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#FFFFFF', EndColorStr='#EEEEEE')";
			    savingMessageHtmlDiv.style.border = "3px solid #BBBBBB";

			    document.body.appendChild(savingMessageHtmlDiv);
		    }
    		
		    savingMessageHtmlDiv.innerHTML = 
				    "<table style=\"width:100%;height:100%;\"><tr><td align=\"center\">" 
				    + "<img align=\"absMiddle\" style=\"width:68px;height:68px\" src=\"" + CommonUrls.WorkItemSaveProgress + "\"><br><br>"
				    + "<font class=\"h3\">" + message + "</font>"
				    + "</td></tr></table>";
    				
		    savingMessageHtmlDiv.style.left = (document.body.clientWidth - 250) / 2;
		    savingMessageHtmlDiv.style.top = (document.body.clientHeight - 150) / 2;
	    }
    	
	    function HideSavingMessage()
	    {
		    if(savingMessageDiv)
		    {
			    savingMessageDiv.style.left = "-5000px";
			    savingMessageDiv.style.top = "-5000px";
			    JsUtility.hideShim(savingShim);
		    }
		    if(savingMessageHtmlDiv)
		    {
			    savingMessageHtmlDiv.style.left = "-5000px";
			    savingMessageHtmlDiv.style.top = "-5000px";
		    }
	    }
	    
	    function OnCallbackResult(arg, context)
	    {
	        HideSavingMessage();
	        
	        var callbackResult = JsUtility.evalCallbackResult(arg);
	        
	        for (key in changeQueue)
            {            
                if (changeQueue[key] != null)
                {
                    var textBox = document.getElementById(timeEntryMetadata[key].clientID);
                    
                    textBox.style.backgroundColor = "";
                    timeEntryMetadata[key].originalValue = changeQueue[key];
                    timeEntryMetadata[key].previousValue = changeQueue[key];
                }
            }
            
            if (callbackResult[0].Result.Problems)
            {
                var problemsList = document.getElementById("problemsList");
                problemsList.innerHTML = "";
                
                for (var i = 0; i < callbackResult[0].Result.Problems.length; i++)
                    problemsList.innerHTML += callbackResult[0].Result.Problems[i];
                    
                problemsPanel.style.display = "block";
            }
            
            else
            {
                problemsPanel.style.display = "none";
            }
                
            if (callbackResult[0].Result.Completed == "No")
            {
                completedNoButton.checked = true;
                completed = "No";
            }
            
            completedYesButton.disabled = callbackResult[0].Result.DisableCompleted;
            completedNoButton.disabled = callbackResult[0].Result.DisableCompleted;
            
            if (callbackResult[0].Result.GridContent)
                document.getElementById("dataGridContainer").innerHTML = callbackResult[0].Result.GridContent;
                
            if (callbackResult[0].Result.EvalContent)
                eval(callbackResult[0].Result.EvalContent);
            
            changeQueue = new Array();
            completedInitialState = completed;
            approvedInitialState = approved;
            completed = "";
            approved = "";
	    }
	    
	    function OnCallbackResultError()
	    {
	        HideSavingMessage();
	    }
        
        function PerformSave()
        {
            var changedEntries = "";
        
            for (key in changeQueue)
            {            
                if (changeQueue[key] != null)
                    changedEntries += key + "," + changeQueue[key] + ",";
            }
            
            if (changedEntries != "")
                changedEntries = changedEntries.substring(0, changedEntries.length - 1);
            
            ShowSavingMessage("Saving changes...");
            JsUtility.initCallback();
	        JsUtility.executeGeneralCallback(timesheetCallbackReference, "command=save&completed=" + completed + "&approved=" + approved + "&changes=" + encodeURIComponent(changedEntries), null, OnCallbackResult, OnCallbackResultError);
        }
    
        function AddChangeToQueue(workItemId, dayIndex, textBox, fieldName)
        {
            var decimalRegex = /^\d(\.\d+){0,1}$/;
            var timeRegex = /^\d{1,2}:[0-5]\d{1}$/;
            var metadataKey = workItemId + "_" + dayIndex + "_" + fieldName;
            
            if (!timeRegex.test(textBox.value) && textBox.value != "")
            {
                if (decimalRegex.test(textBox.value))
                {
                    var hours = Math.floor(textBox.value);
                    var minutes = Math.round((textBox.value - Math.floor(textBox.value)) * 60);
                    
                    if (minutes < 10)
                        minutes = "0" + minutes;
                    
                    textBox.value = hours + ":" + minutes;
                }
            
                else
                {
                    textBox.focus();
                    alert("Please enter your time in HH:MM format.");
                    return;
                }
            }
        
            if (textBox.value == timeEntryMetadata[metadataKey].originalValue)
            {
                textBox.style.backgroundColor = "";
                changeQueue[metadataKey] = null;
            }
            
            else
            {
                changeQueue[metadataKey] = textBox.value + "_" + (convertFromTimeString(textBox.value) - convertFromTimeString(timeEntryMetadata[metadataKey].originalValue));
                textBox.style.backgroundColor = "#dbacac";
            }
            
            var previousValue = timeEntryMetadata[metadataKey].previousValue;
            var difference = convertFromTimeString(textBox.value) - convertFromTimeString(previousValue);
            
            document.all("day" + dayIndex.toString() + "Total").innerHTML = convertToTimeString(convertFromTimeString(document.all("day" + dayIndex.toString() + "Total").innerHTML) + difference);
            document.all("day7Total").innerHTML = convertToTimeString(convertFromTimeString(document.all("day7Total").innerHTML) + difference);
            
            if (document.all("workItem" + workItemId.toString() + "Total"))
                document.all("workItem" + workItemId.toString() + "Total").innerHTML = convertToTimeString(convertFromTimeString(document.all("workItem" + workItemId.toString() + "Total").innerHTML) + difference);
            
            if (document.all("workItem" + workItemId.toString() + fieldName + "Total"))
                document.all("workItem" + workItemId.toString() + fieldName + "Total").innerHTML = convertToTimeString(convertFromTimeString(document.all("workItem" + workItemId.toString() + fieldName + "Total").innerHTML) + difference);
                
            if (document.all("workItem" + workItemId.toString() + "Day" + dayIndex.toString() + "Total"))
                document.all("workItem" + workItemId.toString() + "Day" + dayIndex.toString() + "Total").innerHTML = convertToTimeString(convertFromTimeString(document.all("workItem" + workItemId.toString() + "Day" + dayIndex.toString() + "Total").innerHTML) + difference);
            
            timeEntryMetadata[metadataKey].previousValue = textBox.value;
        }
        
        function convertFromTimeString(value) {
            if (value == "")
                return 0;
        
            var hours = parseInt(value.substr(0, value.indexOf(":")));
            var minutes = parseInt(value.substr(value.indexOf(":") + 1));

            return hours + (minutes / 60);
        }
        
        function convertToTimeString(value) {
            var minutes = (value - Math.floor(value)) * 60;
            minutes = minutes.toString();
            
            if (minutes.length == 1)
                minutes = "0" + minutes;
            
            return Math.floor(value).toString() + ":" + minutes;
        }
    </script>
        
    <asp:Literal ID="titleLiteral" runat="server" />

    <asp:Panel ID="problemsPanel" runat="server" CssClass="err" Style="display: none">
        <asp:Literal ID="problemsLiteral" runat="server" />
    </asp:Panel>

    <asp:Panel ID="contentPanel" runat="server">
        <div style="width: 100%; vertical-align: middle">
            <center>
        	    <table cellpadding="6" cellspacing="0" border="0">
        		    <tr>
            		    <td valign="bottom">
            			    <asp:HyperLink runat="server" ID="previousWeek"><img src="/Resources/Images/mleft.gif" border="0" valign="bottom" /></asp:HyperLink>
            		    </td>
            		    <td valign="top">
            			    <h3><font color="#555555">Week beginning <asp:Label ID="startOfWeekLabel" runat="server" /></font></h3>
            		    </td>
            		    <td valign="bottom">
            			    <asp:HyperLink runat="server" ID="nextWeek"><img src="/Resources/Images/mright.gif" border="0" valign="bottom" /></asp:HyperLink>
            		    </td>
            	    </tr>
                </table>
            </center>
        </div>
        
        <div id="dataGridContainer">
            <asp:DataGrid ID="timesheetDataGrid" runat="server" AutoGenerateColumns="False" Width="100%" HeaderStyle-HorizontalAlign="Center" ShowFooter="true" OnItemDataBound="dataGridItem_DataBound" CellPadding="3" ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center" BorderStyle="Solid" BorderWidth="1" BorderColor="#DDDDDD" EnableViewState="false">
                <Columns>
                    <asp:TemplateColumn ItemStyle-HorizontalAlign="Left" FooterStyle-HorizontalAlign="Left">
                        <HeaderTemplate>
                            Work Item
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Literal ID="rowHeader" runat="server" OnDataBinding="timesheetRowHeader_DataBinding" />
                        </ItemTemplate>
                        <FooterTemplate>
                            Total
                        </FooterTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:TextBox EnableViewState="false" ID="sundayTime" runat="server" Columns="4" OnDataBinding="timesheetEntry_DataBinding" OnPreRender="textBox_PreRender" />
                            <asp:Literal ID="sundayTimeReadOnly" runat="server" OnDataBinding="timesheetEntry_DataBinding" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:TextBox EnableViewState="false" ID="mondayTime" runat="server" Columns="4" OnDataBinding="timesheetEntry_DataBinding" OnPreRender="textBox_PreRender" />
                            <asp:Literal ID="mondayTimeReadOnly" runat="server" OnDataBinding="timesheetEntry_DataBinding" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:TextBox EnableViewState="false" ID="tuesdayTime" runat="server" Columns="4" OnDataBinding="timesheetEntry_DataBinding" OnPreRender="textBox_PreRender" />
                            <asp:Literal ID="tuesdayTimeReadOnly" runat="server" OnDataBinding="timesheetEntry_DataBinding" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:TextBox EnableViewState="false" ID="wednesdayTime" runat="server" Columns="4" OnDataBinding="timesheetEntry_DataBinding" OnPreRender="textBox_PreRender" />
                            <asp:Literal ID="wednesdayTimeReadOnly" runat="server" OnDataBinding="timesheetEntry_DataBinding" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:TextBox EnableViewState="false" ID="thursdayTime" runat="server" Columns="4" OnDataBinding="timesheetEntry_DataBinding" OnPreRender="textBox_PreRender" />
                            <asp:Literal ID="thursdayTimeReadOnly" runat="server" OnDataBinding="timesheetEntry_DataBinding" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:TextBox EnableViewState="false" ID="fridayTime" runat="server" Columns="4" OnDataBinding="timesheetEntry_DataBinding" OnPreRender="textBox_PreRender" />
                            <asp:Literal ID="fridayTimeReadOnly" runat="server" OnDataBinding="timesheetEntry_DataBinding" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:TextBox EnableViewState="false" ID="saturdayTime" runat="server" Columns="4" OnDataBinding="timesheetEntry_DataBinding" OnPreRender="textBox_PreRender" />
                            <asp:Literal ID="saturdayTimeReadOnly" runat="server" OnDataBinding="timesheetEntry_DataBinding" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <span id='workItem<%# Eval("workItemId") %><%# ((bool)Eval("isTotalRow") ? "" : Eval("fieldName")) %>Total'><%# ConvertToTimeString(Convert.ToDecimal(Eval("total")), true) %></span>
                        </ItemTemplate>
                        <HeaderTemplate>Total</HeaderTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>
        </div>
        
        <table cellpadding="0" cellspacing="0" border="0">
            <tr>
                <td align="right">Completed:</td>
                <td>
                    <asp:RadioButtonList ID="completedRadioButtonList" runat="server" RepeatDirection="Horizontal">
                        <asp:ListItem Text="Yes" Value="Yes" />
                        <asp:ListItem Text="No" Value="No" Selected="true" />
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <td align="right" valign="top" style="padding-top: 5px">Approved:</td>
                <td valign="top">
                    <asp:RadioButtonList ID="approvedRadioButtonList" runat="server" RepeatDirection="Horizontal">
                        <asp:ListItem Text="Yes" Value="Yes" />
                        <asp:ListItem Text="No" Value="No" />
                    </asp:RadioButtonList>
                    <asp:Panel ID="commentsPanel" runat="server" Visible="false">
                        Comments<asp:Literal ID="commentsSourceLiteral" runat="server" Visible="false" />:<br />
                        <asp:TextBox ID="commentsTextBox" runat="server" Visible="false" Rows="4" Columns="60" TextMode="MultiLine" />
                        <asp:Literal ID="commentsLiteral" runat="server" Visible="false">(None)</asp:Literal>
                    </asp:Panel>
                </td>
            </tr>
        </table>
    </asp:Panel>
</asp:Content>